package Service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.portlet.ActionRequest;
import javax.xml.rpc.ServiceException;

import org.springframework.web.multipart.MultipartRequest;

import edu.sdsc.nbcr.opal.AppServiceLocator;
import edu.sdsc.nbcr.opal.AppServicePortType;
import edu.sdsc.nbcr.opal.FaultType;
import edu.sdsc.nbcr.opal.InputFileType;
import edu.sdsc.nbcr.opal.JobInputType;
import edu.sdsc.nbcr.opal.JobSubOutputType;

public class LaunchJobHelper {

	private String cmd = "";
	private String outURL = "";
	
	public void doLaunch(String selectedURL,ActionRequest request,GetServerInfo sinfo){
					
			//this will contains all the informations passes to service
			JobInputType in = new JobInputType();
			
			//call CMDHelper's buildCMD to build the correct cmd to submit
			CMDHelper helper = new CMDHelper();
			cmd = helper.buildCMD(sinfo, request);//the command to launch job
			int numoffiles = helper.getNumoffile();//number of input files
			ArrayList<String> filenames = helper.getFilenames();//names of input files

			//when there are file uploaded
			if(filenames.size() != 0 ){
				MultipartRequest multiRequest = (MultipartRequest)request;
		       	Iterator it = multiRequest.getFileNames();
		        InputFileType[] files = new InputFileType[numoffiles]; 
		        int i = 0;
		        while(it.hasNext()){
		        	
		        	//the name of file not the orignal file name
		        	String filename = (String)it.next();
		        	
		        	//check if that file is uploaded
		        	if(filenames.contains(filename)){
				        try {
				        	InputFileType file = new InputFileType();
				        	file.setName(multiRequest.getFile(filename).getOriginalFilename());
				        	file.setContents(multiRequest.getFile(filename).getBytes());
				        	files[i] = file; 
				        	i++;
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} 
		        	}
		        }

		        //add files into jobinputtype
			    in.setInputFile(files);
			}			
		    in.setArgList(cmd); 

		    JobSubOutputType subOut = null;
		
		    try {
		        AppServiceLocator asl = new AppServiceLocator();
		        AppServicePortType appServicePort = asl.getAppServicePort(new URL(selectedURL));

		        // TODO: fix this to use attachments, if need be
		        subOut = appServicePort.launchJob(in);
		    
		        outURL = subOut.getJobID();
		    }catch(FaultType e){
		    	outURL = "Error during Submission";
		    	e.printStackTrace();
		    } catch (RemoteException e) {
		    	outURL = "Error during Submission";
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
		    	outURL = "Error during Submission";
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				outURL = "Error during Submission";
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public String getCmd() {
		return cmd;
	}

	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

	public String getOutURL() {
		return outURL;
	}

	public void setOutURL(String outURL) {
		this.outURL = outURL;
	}
	
	
}
